package com.leyou.user.controller;

import com.leyou.user.dto.UserDTO;
import com.leyou.user.entity.User;
import com.leyou.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("/info")
public class UserController {

    /**
     * 数据唯一性校验
     * @param data
     * @param type
     * @return
     */
    @Autowired
    private UserService userService;

    @RequestMapping("/exists/{data}/{type}")
    public ResponseEntity<Boolean> QueryDataIxist(
            @PathVariable("data") String data ,
            @PathVariable("type") Integer type
    ){

        return ResponseEntity.ok(this.userService.QueryDataIxist(data,type));
    }

    /**
     * 发送短信验证码
     * @param phone
     * @return
     */
    @PostMapping("/code")
    public ResponseEntity<Void> sendVerifyCode(@RequestParam ("phone") String phone){

        this.userService.sendVerifyCode(phone);
        return ResponseEntity.ok().build();
    }

    /**
     * 注册用户
     * @param user 用户信息
     * @param code 验证码
     * @return 无
     */
    @PostMapping
    public ResponseEntity<Void> register(User user ,@RequestParam("code") String code){

        this.userService.register(user,code);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 根据手机号和密码查询用户
     * @param username 手机号
     * @param password 密码
     * @return 用户信息
     */
    @RequestMapping
    public ResponseEntity<UserDTO> queryByPhoneAndPassword(
            @RequestParam("username") String username,
            @RequestParam("password") String password){

        return  ResponseEntity.ok(this.userService.queryByPhoneAndPassword(username,password));
    }


}
